import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

class ScrollNotificationTestRoute extends StatefulWidget {
  const ScrollNotificationTestRoute({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _ScrollNotificationTestRoute();
}

class _ScrollNotificationTestRoute extends State<ScrollNotificationTestRoute> {
  final logger = Logger(printer: PrettyPrinter());

  int _progress = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('监听滚动通知'),
      ),
      body: Scrollbar(
        child: NotificationListener<ScrollNotification>(
          onNotification: (ScrollNotification n) {
            setState(() {
              _progress = ((n.metrics.pixels / n.metrics.maxScrollExtent) * 100)
                  .toInt();
            });
            logger.i("BottomEdge: ${n.metrics.extentAfter == 0}");
            return false;
          },
          child: Stack(
            alignment: Alignment.center,
            children: [
              ListView.builder(
                itemCount: 100,
                itemExtent: 50.0,
                itemBuilder: (context, index) =>
                    ListTile(title: Text("$index")),
              ),
              CircleAvatar(
                radius: 30,
                child: Text("$_progress%"),
                backgroundColor: Colors.black54,
              )
            ],
          ),
        ),
      ),
    );
  }
}
